/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.biz.manager.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.grayrabbit.pay.core.model.results.GenericsResult;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.biz.manager.system.AppSystemManager;
import org.grayrabbit.pay.core.model.entity.system.AppSystem;
import org.grayrabbit.pay.core.model.entity.system.AssAppsystemTradeterminal;
import org.grayrabbit.pay.core.service.system.AppSystemService;
import org.grayrabbit.pay.core.service.system.AssAppsystemTradeterminalService;
import org.grayrabbit.pay.core.service.transaction.TradeHistoryService;
import org.grayrabbit.pay.core.service.transaction.TradeRecordHistoryService;
import org.grayrabbit.pay.core.service.transaction.TradeRecordService;
import org.grayrabbit.pay.core.service.transaction.TradeService;

/**
 * 
 * 
 * @author liyd
 * @version $Id: AppSystemManagerImpl.java, v 0.1 Fri Jun 01 11:14:01 CST 2012
 *          liyd Exp $
 */
@Component
public class AppSystemManagerImpl implements AppSystemManager {

    @Autowired
    private AppSystemService                 appSystemService;

    @Autowired
    private TradeService                     tradeService;

    @Autowired
    private TradeHistoryService              tradeHistoryService;
    @Autowired
    private TradeRecordService               tradeRecordService;

    @Autowired
    private TradeRecordHistoryService        tradeRecordHistoryService;

    @Autowired
    private AssAppsystemTradeterminalService assAppSystemTradeTerminalService;

    /**
     * 查询业务列表
     * 
     * @param district
     * @return
     */
    public GenericsResult<PageList<AppSystem>> queryPageList(AppSystem appSystem) {

        GenericsResult<PageList<AppSystem>> result = new GenericsResult<PageList<AppSystem>>(true);

        result.setResultData(appSystemService.queryPageList(appSystem));

        return result;
    }

    /**
     * 根据Id得到实体对象
     * 
     * @param id
     * @return
     */
    public GenericsResult<AppSystem> getById(Long id) {

        GenericsResult<AppSystem> result = new GenericsResult<AppSystem>(true);

        result.setResultData(appSystemService.getById(id));

        return result;

    }

    public PayResult getIfRepeatByCode(AppSystem appSystem) {

        PayResult result = new PayResult(true);
        int num = appSystemService.getCountByAppCode(appSystem.getAppCode());
        if (num > 0) {
            result.setSuccess(false);
            result.setResultMsg("系统code重复了！");
        }
        return result;

    }

    /**
     * 根据id批量删除
     * 
     * @param idArray
     * @return
     */
    public PayResult deleteBatchById(Long[] idArray) {

        PayResult result = new PayResult(true);
        List<Long> trueList = new ArrayList<Long>();

        StringBuffer names = new StringBuffer(",");
        for (Long id : idArray) {
            if (tradeService.getBySystemId(id).size() > 0
                || tradeHistoryService.getBySystemId(id).size() > 0
                || tradeRecordService.getBySystemId(id).size() > 0
                || tradeRecordHistoryService.getBySystemId(id).size() > 0) {
                names.append(appSystemService.getById(id).getAppName());
                names.append(",");
            } else {
                trueList.add(id);
            }
        }
        Long[] trueArray = new Long[trueList.size()];
        trueArray = trueList.toArray(trueArray);
        if (trueArray.length > 0) {
            assAppSystemTradeTerminalService.deleteBatchBySystemIds(trueArray);
            appSystemService.deleteBatchById(trueArray);
        }
        if (trueArray.length < idArray.length) {
            result.setSuccess(false);
            result.setResultMsg("系统:" + names.substring(1) + "有外键约束，不能删除！");
        }

        return result;
    }

    /**
     * 查询所有对象
     * 
     * @return
     */
    public GenericsResult<List<AppSystem>> findAll() {

        GenericsResult<List<AppSystem>> result = new GenericsResult<List<AppSystem>>(true);

        result.setResultData(appSystemService.findAll());

        return result;
    }

    public GenericsResult<Long[]> findTerminalIds(Long id) {

        GenericsResult<Long[]> result = new GenericsResult<Long[]>(true);
        List<Long> list = assAppSystemTradeTerminalService.findBySystemId(id);
        Long[] ids = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ids[i] = list.get(i);
        }
        result.setResultData(ids);
        return result;
    }

    public GenericsResult<Map<String, AppSystem>> findAllMap() {

        GenericsResult<Map<String, AppSystem>> result = new GenericsResult<Map<String, AppSystem>>(
            true);
        List<AppSystem> appList = appSystemService.findAll();
        Map<String, AppSystem> appMap = new HashMap<String, AppSystem>();
        for (AppSystem app : appList) {
            appMap.put(app.getAppCode(), app);
        }
        result.setResultData(appMap);

        return result;
    }

    /**
     * 添加一个对象
     * 
     * @param appSystem
     * @return
     */
    public GenericsResult<AppSystem> insert(AppSystem appSystem) {

        GenericsResult<AppSystem> result = new GenericsResult<AppSystem>(true);
        int num = appSystemService.getCountByAppCode(appSystem.getAppCode());
        if (num > 0) {
            result.setSuccess(false);
            result.setResultMsg("系统code重复！");
        } else
            appSystemService.insert(appSystem);

        return result;

    }

    public GenericsResult<Integer> insertAss(Long[] terminalIds, Long systemId) {
        GenericsResult<Integer> result = new GenericsResult<Integer>(true);
        for (Long terminalId : terminalIds) {
            AssAppsystemTradeterminal assAppsystemTradeterminal = new AssAppsystemTradeterminal();
            assAppsystemTradeterminal.setSystemId(systemId);
            assAppsystemTradeterminal.setTerminalId(terminalId);
            assAppSystemTradeTerminalService.insert(assAppsystemTradeterminal);
        }

        return result;

    }

    /**
     * 更新一个对象
     * 
     * @param appSystem
     * @return
     */
    public GenericsResult<Integer> update(AppSystem appSystem) {

        GenericsResult<Integer> result = new GenericsResult<Integer>(true);

        result.setResultData(appSystemService.update(appSystem));

        return result;
    }

    public GenericsResult<Integer> saveStatus(AppSystem appSystem) {

        GenericsResult<Integer> result = new GenericsResult<Integer>(true);

        result.setResultData(appSystemService.changeStatus(appSystem));

        return result;
    }

    /**
     * 删除一个对象
     * 
     * @param appSystem
     * @return
     */
    public PayResult delete(AppSystem appSystem) {

        PayResult result = new PayResult(true);

        appSystemService.delete(appSystem);

        return result;
    }

    public PayResult deleteBySystemId(Long systemId) {

        PayResult result = new PayResult(true);
        assAppSystemTradeTerminalService.deleteBatchBySystemId(systemId);

        return result;
    }

}
